const app = getApp();

Page({
  data: {
    phone: '',
    code: '',
    countdown: 0,
    isCodeSent: false,
    interval: null,
    redirectUrl: '/pages/index/index',
    isLogin: false,
    timeLeft: 3
  },

  onLoad: function(options) {
    console.log('Login page loaded', options);
    // If we need to redirect after login
    this.setData({
      redirectUrl: options.redirect || '/pages/index/index'
    });

    // 查看用户是否已登录
    this.checkLoginStatus();
  },

  onUnload: function() {
    // Clear interval when page unloads
    if (this.data.interval) {
      clearInterval(this.data.interval);
    }
  },

  // 检查登录状态
  checkLoginStatus: function () {
    const isLogin = app.globalData.isLogin;
    if (isLogin) {
      this.setData({
        isLogin
      });
      // 已登录，倒计时返回
      let timeLeft = 3;
      this.setData({
        timeLeft
      });
      
      const timer = setInterval(() => {
        timeLeft--;
        this.setData({
          timeLeft
        });
        
        if (timeLeft <= 0) {
          clearInterval(timer);
          wx.switchTab({
            url: '/pages/user/user'
          });
        }
      }, 1000);
    }
  },

  // 输入手机号
  onPhoneInput: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },

  // 输入验证码
  onCodeInput: function(e) {
    this.setData({
      code: e.detail.value
    });
  },

  // 发送验证码
  sendCode: function() {
    if (this.data.countdown > 0 || !this.data.phone || this.data.phone.length !== 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }

    // 开始倒计时
    this.setData({
      countdown: 60,
      isCodeSent: true
    });

    // 模拟验证码发送成功
    wx.showToast({
      title: '验证码已发送',
      icon: 'success'
    });

    // 设置倒计时
    const interval = setInterval(() => {
      if (this.data.countdown <= 1) {
        clearInterval(interval);
        this.setData({
          countdown: 0,
          interval: null,
          isCodeSent: false
        });
      } else {
        this.setData({
          countdown: this.data.countdown - 1
        });
      }
    }, 1000);

    this.setData({
      interval: interval
    });
  },

  // 手机号登录
  login: function() {
    const { phone, code } = this.data;
    
    if (!phone || phone.length !== 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }

    if (!code || code.length !== 6) {
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '登录中...',
    });

    // 模拟登录过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 登录成功，保存用户信息
      const userInfo = {
        nickName: '用户' + phone.substring(7),
        avatarUrl: '/images/default-avatar.png',
        memberLevel: '普通会员',
        points: 100,
        balance: 0
      };
      
      // 保存登录状态
      wx.setStorageSync('isLoggedIn', true);
      wx.setStorageSync('userInfo', userInfo);
      
      // 更新全局状态
      app.globalData.isLogin = true;
      app.globalData.userInfo = userInfo;
      
      // 添加测试数据到购物车
      app.globalData.cartList = [
        {
          id: 1,
          name: '时尚连衣裙',
          price: 199.00,
          quantity: 1,
          selected: true,
          imageUrl: 'https://img01.yzcdn.cn/vant/cat.jpeg',
          spec: '红色, M码'
        },
        {
          id: 2,
          name: '休闲运动鞋',
          price: 299.00,
          quantity: 1,
          selected: true,
          imageUrl: 'https://img01.yzcdn.cn/vant/cat.jpeg',
          spec: '白色, 39码'
        }
      ];
      
      // 更新页面状态
      this.setData({
        isLogin: true
      });
      
      // 显示登录成功并倒计时返回
      let timeLeft = 3;
      this.setData({
        timeLeft
      });
      
      const timer = setInterval(() => {
        timeLeft--;
        this.setData({
          timeLeft
        });
        
        if (timeLeft <= 0) {
          clearInterval(timer);
          wx.switchTab({
            url: '/pages/user/user'
          });
        }
      }, 1000);
    }, 1500);
  },

  // 微信一键登录
  onGotUserInfo: function(e) {
    // 新版本微信不再支持open-type="getUserInfo"，我们使用模拟数据
    wx.showLoading({
      title: '登录中...',
    });

    // 模拟登录过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 登录成功，保存用户信息
      const userInfo = {
        nickName: '微信用户',
        avatarUrl: '/images/default-avatar.png',
        memberLevel: '普通会员',
        points: 100,
        balance: 0
      };
      
      // 保存登录状态
      wx.setStorageSync('isLoggedIn', true);
      wx.setStorageSync('userInfo', userInfo);
      
      // 更新全局状态
      app.globalData.isLogin = true;
      app.globalData.userInfo = userInfo;
      
      // 添加测试数据到购物车
      app.globalData.cartList = [
        {
          id: 1,
          name: '时尚连衣裙',
          price: 199.00,
          quantity: 1,
          selected: true,
          imageUrl: 'https://img01.yzcdn.cn/vant/cat.jpeg',
          spec: '红色, M码'
        },
        {
          id: 2,
          name: '休闲运动鞋',
          price: 299.00,
          quantity: 1,
          selected: true,
          imageUrl: 'https://img01.yzcdn.cn/vant/cat.jpeg',
          spec: '白色, 39码'
        }
      ];
      
      // 更新页面状态
      this.setData({
        isLogin: true
      });
      
      // 显示登录成功并倒计时返回
      let timeLeft = 3;
      this.setData({
        timeLeft
      });
      
      const timer = setInterval(() => {
        timeLeft--;
        this.setData({
          timeLeft
        });
        
        if (timeLeft <= 0) {
          clearInterval(timer);
          wx.switchTab({
            url: '/pages/user/user'
          });
        }
      }, 1000);
    }, 1500);
  }
}) 